import sys

import pygame
from pygame.sprite import Group
from setttings import Settings
from ship import Ship
import game_functions as gf
from game_stats import GameStats
from button import Button
from scoreboard import ScoreBoard


def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption('外星人入侵')
    # 创建开始游戏按钮
    play_button = Button(ai_settings, screen, '开始游戏')
    # 创建统计信息实例
    stats = GameStats(ai_settings)
    # 创建计分板
    scoreboard = ScoreBoard(ai_settings, stats, screen)
    ship = Ship(screen, ai_settings)
    # alien = Alien(ai_settings, screen)
    aliens = Group()

    """创建一个用于存储子弹的编组，这个类似于列表，pygame提供的Group类"""
    bullets = Group()

    """创建外星人群"""
    gf.create_fleet(ai_settings, screen, aliens, ship)
    while True:
        """将子弹传入事件监听和屏幕刷新中使用"""
        gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets)

        if stats.game_active:
            gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets)
            gf.update_bullets(ai_settings, screen, ship, bullets, aliens)
            ship.update()

        gf.update_screen(ai_settings, stats, scoreboard, screen, ship, aliens, bullets, play_button)


if __name__ == '__main__':
    run_game()
